var page = 1;
var pagenum = 10;
var fi = 1
var url = api.menu
var main = [
	[ //表头
		 {
			field: 'name2',
			title: '菜单名',
			sort: true,
			fixed: 'left'
		}, {
			field: 'url',
			title: '路径',
			sort: true
		}, {
			field: 'icon',
			width: 200,
			title: '图标'
		}, {
			field: 'state2',
			width: 200,
			title: '状态'
		}, {
			title: '操作',
			width: 250,
			toolbar: '#operation',
			fixed: 'right'
		}
	]
];
var data = {};
data.module_type=0;
data.is_tree=1
ajaxdriver(data)

function ajaxdriver(data) {
	console.log(data)
	Load()
	ajax(api.menu.list, data, 'post').done(function(res) {
		console.log(res)
		Load(1)
		var tree=[]
		if(res.code == 1) {
			console.log(res)
			for(var i1=0;i1<res.data.length;i1++){
				res.data[i1].name2=res.data[i1].name
				tree.push(res.data[i1])
				for(var i2=0;i2<res.data[i1].children.length;i2++){
					res.data[i1].children[i2].name2='├'+res.data[i1].children[i2].name
					tree.push(res.data[i1].children[i2])
					for(var i3=0;i3<res.data[i1].children[i2].children.length;i3++){
						res.data[i1].children[i2].children[i3].name2='├├'+res.data[i1].children[i2].children[i3].name
						tree.push(res.data[i1].children[i2].children[i3])
					}
				}
			}
			
			
			for(var ii=0;ii<tree.length;ii++){
				if(tree[ii].state==1){
					tree[ii].state2='启用'
				}else{
					tree[ii].state2='禁用'
				}
			}
				console.log(tree)
			table.render({
				elem: '#driver',
				cellMinWidth: 80,
				cols: main,
				data: tree,
				limit: 999,
				even: true
			});
			
			table.on('tool(driver)', function(obj) {
				var data1 = obj.data; //获得当前行数据
				var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
				var tr = obj.tr; //获得当前行 tr 的DOM对象
				if(layEvent === 'del') { //删除
					layer.confirm('是否删除', function(index) {
						Load()
						ajax(api.menu.delete,{
							ids: data1.id
						},'post').done(function(res) {
							if(res.code == 1) {
									ajaxdriver(data)
									layer.close(index);
									Load(1)

								} else {
									layer.close(index);
									Load(1)
									Err(res.msg ? res.msg : '操作失败，稍候再试');
								}
						}).fail(function(err) {
							Load(1)
							Err('请求失败')
						});
						
						
						
						layer.close(index);
					});
				} else if(layEvent === 'edit') { //编辑
					//do something
					// window.location.href = " {:url('/html/system/menu_add')}?id=" + data1.id
					//同步更新缓存对应的值
					var url = "{:url('/html/system/menu_add')}?op=search&id="+data1.id;
					layer.open({
						type: 2,
						title:'请编辑',
						shadeClose: true,
						shade: 0.8,
						area: ['80%','80%'],
						content: url //iframe的url
					  })

				}  else if(layEvent === 'forbidden') {
					layer.confirm('是否禁用', function(index) {
						Load();
						data1.state=9;
						delete data1.sort
						ajax(api.menu.edit,data1,'post').done(function(res) {
							if(res.code == 1) {
								ajaxdriver(data)
									layer.close(index);
									Load(1)

								} else {
									layer.close(index);
									Load(1)
									Err(res.msg ? res.msg : '操作失败，稍候再试');
								}
						}).fail(function(err) {
							Load(1)
							Err('请求失败')
						});
						layer.close(index);
					});
				} else if(layEvent === 'startusing') {
					layer.confirm('是否启用', function(index) {
						Load()
						data1.state=1;
						delete data1.sort
						console.log(api.menu.edit)
						ajax(api.menu.edit,data1,'post').done(function(res) {
							if(res.code == 1) {
									ajaxdriver(data)
									layer.close(index);
									Load(1)

								} else {
									layer.close(index);
									Load(1)
									Err(res.msg ? res.msg : '操作失败，稍候再试');
								}
						}).fail(function(err) {
							Load(1)
							Err('请求失败')
						});
						layer.close(index);
					});
				}
			});
			//删除多个

			$(".delete-multiple").on('click', function() {
				var driver_ids = '';
				var checkStatus = table.checkStatus('driver'); //test即为基础参数id对应的值
				console.log(checkStatus.data) //获取选中行的数据
				console.log(checkStatus.data.length) //获取选中行数量，可作为是否有选中行的条件
				console.log(checkStatus.isAll) //表格是否全选
				for(var dei = 0; dei < checkStatus.data.length; dei++) {
					if(dei == 0) {
						driver_ids += checkStatus.data[dei].driver_id
					} else {
						driver_ids += ("," + checkStatus.data[dei].driver_id)
					}
				}
				console.log(driver_ids)
				layer.open({
					type: 1,
					title: false,
					id: "delete",
					content: "<div class='am-vertical-align-middle' style='width:100%;text-align:center'>是否删除</div>",
					btn: ['确定', '取消'],
					btnAlign: 'c' //按钮居中
						,
					shade: 0 //不显示遮罩
						,
					yes: function() {
						var deleteload = layer.load(1, {
							shade: [0.1, '#fff'] //0.1透明度的白色背景
						});
						$.ajax({
							method: "POST",
							url: url.delete,
							data: {
								driver_ids: driver_ids
							},
							success: function(res) {
								console.log(res)
								if(res.code == 1) {
									layer.close(deleteload);
									layer.closeAll();
									Msg('删除成功')
									ajaxdriver(data)
								} else {
									layer.close(deleteload);
									Err(res.msg ? res.msg : '操作失败，稍候再试');
								}

							}
						});

					},
					btn2: function() {
						layer.closeAll();
					}
				});
			});

		} else {
			Err(res.msg)
		}
	}).fail(function(err) {
		Err('请求失败')
	});

}
//删除
$('.delete-list').on('click', function() {
	console.log(1)
	popup(this, url.delete);

});
//添加
$('.add').on('click', function() {
	window.location.href = '{:url("/html/system/menu_add")}'
});
//启用
$('.startusing').on('click', function() {
	popup(this, "{:url('state')}");

});
//禁用
$('.forbidden').on('click', function() {
	popup(this, "{:url('state')}");

});
